package com.cci.kangdao.aop;

import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.HTMLUtils;
import com.gexin.fastjson.JSON;
import lombok.Getter;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.json.JSONTokener;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *Body重写 HttpServletRequestWrapper
 */
@Getter
public class BodyRequestWrapper extends HttpServletRequestWrapper {
    private Logger log = Logger.getLogger(BodyRequestWrapper.class);
    // 存放JSON数据主体
    private String body;

    public BodyRequestWrapper(HttpServletRequest request, String context) {
        super(request);
        body = context;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes("UTF-8"));
        ServletInputStream servletInputStream = new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener listener) {

            }
        };
        return servletInputStream;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }


    /**
     * 解密
     */
    public void getParameterDecrpty(String[] fieldArray) {
        Map<String,Object> results = new HashMap<>();
        try {
            Object json = new JSONTokener(body).nextValue();
            if(json instanceof JSONObject){
                results = JSON.parseObject(body);
            }
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
        if(!results.isEmpty()){
            Iterator<Map.Entry<String, Object>> it = results.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, Object> entry = it.next();
                Object  value = entry.getValue();
                if(value instanceof String){
                    value = HTMLUtils.stringFilter((String) entry.getValue());
                }
                results.put(entry.getKey(),value);
            }
            if(results.containsKey("isEncrypt")
                    && "1".equals(MapUtils.getString(results, "isEncrypt"))){
                for(String field : fieldArray){
                    if(results.containsKey(field)){
                        String decrpty = AESTool.decryptString(MapUtils.getString(results, field));
                        results.put(field,decrpty);
                    }
                }
                body = JSON.toJSONString(results);
            }
        }
    }

}
